package com.shop.controller;

import com.github.pagehelper.PageInfo;
import com.shop.base.BaseController;
import com.shop.entity.Category;
import com.shop.result.ResultVO;
import com.shop.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/category")
public class CategoryController extends BaseController {

    private final CategoryService categoryService;

    /**
     * 获取所有分类
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/listCategory")
    public PageInfo<Category> listCategory(@RequestParam("pageNo") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        return categoryService.listCategory(pageNum, pageSize);
    }

    @GetMapping("/getCategory")
    public Category getCategory(@RequestParam("name") String name){
        return categoryService.getCategory(name);
    }
    /**
     * 添加分类
     *
     * @param category
     * @return
     */
    @PostMapping("/addCategory")
    public ResultVO<Category> addCategory(@RequestBody Category category){
        categoryService.addCategory(category);
        return success(category);
    }
    @PostMapping("/updateCategory")
    public void updateCategory(@RequestBody Category category){
        categoryService.updateCategory(category);
    }

    /**
     * 删除分类
     * @param id
     */
    @GetMapping("/deleteCategory")
    public void deleteCategory(@RequestParam("id") Integer id){
        categoryService.deleteCategory(id);
    }
}
